package com.example.aacwebdemo.filebeat;

import com.example.aacwebdemo.filebeat.log.CustomerLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author zhao.xin06679
 * @date 2021年06月10日 15:30
 */
@RestController
@RequestMapping("/filebeat_test")
public class FileBeatTestController {

    ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

    @RequestMapping(value="mock_logdata",method = RequestMethod.GET)
    public Object mockLogData(){

        service.scheduleAtFixedRate(() ->{
            long time = System.currentTimeMillis();
            for(int i = 0 ; i< 1500; i ++) {
                CustomerLog.logConsumeInfo(time + ":" + i);
            }
        },2,1, TimeUnit.SECONDS);


        return "success";
    }

    @RequestMapping(value="stop_mock_logdata",method = RequestMethod.GET)
    public Object stopMockLogData(){
        service.shutdown();
        return "success";
    }


    public static void main(String[] args) {


        CustomerLog.logConsumeInfo(-1);

        Long start = System.currentTimeMillis();

        for(int i = 0 ; i< 1500; i ++){
            //System.out.println(i);
            CustomerLog.logConsumeInfo(i);
        }

        Long end = System.currentTimeMillis();

        System.out.println((end-start)/1000F);

//        Long start = System.currentTimeMillis();
//
//
//
//        CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {
//
//            @Override
//            public void run() {
//                long end = System.currentTimeMillis();
//                System.out.println(Thread.currentThread().getName() + " 完成最后任务 " + (end - start)/1000F);
//            }
//        });
//
//        TaskThread t1 =new TaskThread(barrier,"1");
//        TaskThread t2 =new TaskThread(barrier,"2");
//        TaskThread t3 =new TaskThread(barrier,"3");
//        t1.start();
//        t2.start();
//        t3.start();



    }

    static class TaskThread extends Thread {

        private CyclicBarrier barrier;
        private String target;

        public TaskThread(CyclicBarrier barrier,String target) {
            this.barrier = barrier;
            this.target = target;
        }

        @Override
        public void run() {
            try{
                for (int i = 0 ; i < 500;i++){
                    CustomerLog.logConsumeInfo(i +":" + target);
                }
                barrier.await();
            }catch (Exception e){
            }

        }
    }


}
